// pages/dj/dj_tongji/dj_tongji.js
var app = getApp();
var id;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    wjnr: [],
    id: 0,
    djsl:0,
    tmsj:[],
    wdxx:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '获取问卷数据中',
    });
    if (options.id != undefined) {
      id = options.id;
      app.globalData.now_wj = [];
      var that = this;
      const db = wx.cloud.database()
      //获取问卷
      db.collection('wenjuan').where({
        _id: options.id
      })
        .get({
          success: res => {
            {
              console.log(res);
              if (res.data.length > 0) {
                console.log(res);
                var wj_info = res.data[0].wj_info;
                console.log(wj_info);
                that.setData({
                  bt: wj_info[0].wj_jbxx.bt,
                  wjnr: wj_info,
                  id:id,
                });
                app.globalData.now_wj = wj_info;
                app.globalData.now_cfdj = wj_info[0].wj_jbxx.cfdj;
                app.globalData.now_xgdj = wj_info[0].wj_jbxx.xgdj;
                app.globalData.now_id = res.data[0]._id;
              }
              else {
                wx.hideLoading();
                wx.showModal({
                  content: '该问卷不存在',
                  showCancel: false,
                  success: function () {
                    wx.navigateBack({ changed: true });//返回上一页  
                  }
                });
              }
            }
          },
          fail: err => {
            wx.hideLoading();
            console.error('[数据库] [新增记录] 失败：', err)
          }
        })
      //获取答卷
      db.collection('user_dt').where({
        wjbh: options.id
      })
        .get({
          success: res => {
            {
              console.log(res);
              if (res.data.length > 0) {
                that.setData({
                  djsl: res.data.length
                })
                //解析答卷
                that.jsdj(res.data);
              }
              else {
                
              }
            }
          },
          fail: err => {
            wx.hideLoading();
            console.error('[数据库] [查询记录] 失败：', err)
          }
        })
    }
    else {
      wx.hideLoading();
      wx.showModal({
        content: '问卷参数丢失,请重试!',
        showCancel: false,
        success: function () {
          wx.redirectTo({
            url: "/pages/index/index",
          })
        }
      });
    }
  },
  jsdj:function(e){
    var that=this;
    console.log(e);
    var ls=[],wdsj=[];
    for(var i=0;i<e.length;i++)
    {
      var xx = e[i].dj_json;
      console.log(xx);
      if (ls[0] == undefined) {
        ls[0] = [];
      }
      if (wdsj[0] == undefined) {
        wdsj[0] = [];
      }
      for(var j=0;j<xx.tm_count;j++)
      {
        if (ls[0][j] == undefined)
        {
          ls[0][j] = [];
        }
        if (wdsj[0][j] == undefined) {
          wdsj[0][j] = [];
        }
        //单选数据处理
        if (xx['tm_lx' + j] == '0')
        {
          if (ls[0][j][xx['tm' + j]] == undefined)
          {
            ls[0][j][xx['tm' + j]] = [];
          }
          if (wdsj[0][j][xx['tm' + j]] == undefined) {
            wdsj[0][j][xx['tm' + j]] = [];
          }
          ls[0][j][xx['tm' + j]].push(xx['tm' + j]);
          if (e[i].openid == app.globalData.openid) {
            wdsj[0][j][xx['tm' + j]].push(xx['tm' + j]);
          }
        }
        //多选数据处理
        if (xx['tm_lx' + j] == '1') {
          var dxsj = xx['tm' + j];
          for(var k=0;k<dxsj.length;k++)
          {
            if (ls[0][j][dxsj[k]] == undefined) {
              ls[0][j][dxsj[k]] = [];
            }
            if (wdsj[0][j][dxsj[k]] == undefined) {
              wdsj[0][j][dxsj[k]] = [];
            }
            ls[0][j][dxsj[k]].push(dxsj[k]);
            if(e[i].openid == app.globalData.openid)
            {
              wdsj[0][j][dxsj[k]].push(dxsj[k]);
            }
          }
        }
      }
    }
    wx.hideLoading();
    console.log(ls);
    console.log(wdsj);
    that.setData({
      tmsj:ls,
      wdsj:wdsj,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

})